/*
 * @Date: 2024-10-31 14:16:21
 * @LastEditors: 阿布 17839092765@163.com
 * @LastEditTime: 2024-10-31 20:55:25
 * @FilePath: \xyh-largescreen-gitee\src\components\GlobalVideo\src\VideoGlobal.ts
 */
/*
 * @Date: 2024-10-31 14:16:21
 * @LastEditors: 阿布 17839092765@163.com
 * @LastEditTime: 2024-10-31 17:13:14
 * @FilePath: \xyh-largescreen-gitee\src\components\GlobalVideo\src\VideoGlobal.ts
 */
/*eslint-disable*/
// @ts-nocheck

import { addMapData, addGeojsonLayer } from './addMapData';
import { _onEvent } from './useEvent';
import { addPopUp } from './addPopUp';

let api: any;

function _onReady() {
  setCamera('0');
  api.geoJSONLayer.clear();
  addMapData(api);
  if (window.location.href.includes('home')) {
  } else {
    // api.geoJSONLayer.clear();
    addGeojsonLayer(api);
  }

  console.log('[ window.location ] >', window.location.href);
}

function initVideo() {
  const url = import.meta.env.VITE_STREAM_URL;
  const id = import.meta.env.VITE_STREAM_ID;
  //初始化API实例并启动渲染，DigitalTwinPlayer对象的构造参数请参考API开发文档
  api = new acapi.DigitalTwinPlayer(url, {
    domId: 'player',
    iid: id, // 灌区的实例id
    apiOptions: {
      onReady: _onReady, //api调用入口，必选回调函数
      onEvent: _onEvent, //可选参数，三维场景交互事件回调函数
    },
  }).getAPI();
}

function destroyVideo() {
  //关闭云渲染并释放资源
  api.destroy();
}

/**
 * 控制是否显示站点
 * @param type 站点类型
 * CZ -> 水位站
 * RR -> 水文站
 * SN -> 枢纽
 * SZ -> 水闸
 * BZ -> 泵站
 * HZ -> 涵闸
 * WQ -> 水质站
 * WSK -> 污水口
 * @param status 显示与否
 */
function showStation(type: string, status: boolean) {
  if (status) {
    api.marker.showByGroupId('marker_group_' + type);
  } else {
    api.marker.hideByGroupId('marker_group_' + type);
  }
}

/**
 * 设置相机定位
 * @param type 0: 默认全局视角 1:新禹河闸 2:三友水库 3:东方红水库
 */
async function setCamera(type: string) {
  // 切换为漫游模式
  fdapi.settings.setInteractiveMode(0);
  addPopUp('xyhz', false); //隐藏新禹河闸弹框

  switch (type) {
    case '0':
      api.camera.set(401998.92125, 3562696.60582, 4845.209375, -22.021889, -92.536469);
      break;
    case '1':
      api.camera.set(404522.619375, 3571968.309375, 24.851274, -7.755379, 179.142639, 2);
      //   setTimeout(() => {
      //     api.camera.set(404514.014688, 3571968.657812, 24.126038, -12.919862, -156.985062);
      //     setTimeout(() => {
      //       // fdapi.settings.setInteractiveMode(1);

      //     }, 2000);
      //   }, 3000);
      setTimeout(() => {
        addPopUp('xyhz', true); //新禹河闸弹框
        // 切换角色视角
        api.settings.setCharacterRoaming(
          [404518.66000000003, 3571968.3200000003, 24],
          [-12.137465, 176.868378, 0],
          4,
        );
      }, 4000);

      break;
    case '2':
      api.camera.set(406120.487969, 3584191.057969, 308.591172, -14.227003, -62.358284);
      break;
    case '3':
      api.camera.set(407023.175625, 3581886.819355, 135.298721, -15.314909, -55.752396);
      break;
  }
}

function getGlobalApi() {
  return api;
}

export { getGlobalApi, initVideo, destroyVideo, showStation, setCamera };
